/**
 * @author 0283000115
 * @date 2018年3月3日-下午2:32:37
 */
package com.zte.claa.inficombo.csa.app.controller;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zte.claa.inficombo.csa.app.controller.ret.ApiResult;
import com.zte.claa.inficombo.csa.app.model.dev.DevBase;
import com.zte.claa.inficombo.csa.app.model.redis.LocMessageBean;
import com.zte.claa.inficombo.csa.app.service.csa.impl.BaseCacheManager;
import com.zte.claa.inficombo.csa.app.service.rptparse.ParserContainer;
import com.zte.claa.inficombo.csa.app.util.JsonUtils;
import com.zte.claa.inficombo.csa.app.util.NumUtils;

import io.swagger.annotations.ApiOperation;

/**
 * @author 0283000115
 * 
 * 该controller仅用于接收克拉GPS定位手机上报的GPS定位坐标数据.
 * 
 */

@RestController
@RequestMapping("/zteclaa/mobilegpsloc") 
public class ClaaMobileGpsLocController extends BaseController {
	// 时间戳格式.
	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	//手机gps定位
	public static final String CLAA_MOBILE_GPS = "claa_mobilegps"; 
	public static final String REPORT_TIME = "reporttime";
	public static final String IMEI_CODE = "imei";	
	public static final String MSISDN = "msisdn";
	public static final String LAT = "gpslat";
	public static final String LNG = "gpslng";
	public static final String ALT = "gpsalt";
	public static final String SPEED = "speed";

		
	// logger
	private Logger logger = LoggerFactory.getLogger(ClaaMobileGpsLocController.class);
	
	@Autowired
	private BaseCacheManager cacheManager;
	
	@Autowired
	private ParserContainer parserContainer;
	
	/**
	 * 定位手机上报的GPS坐标数据上报.
	 * 样例：
	  		{
	  		"reporttime":"2020-04-21 10:24:20",
	  		"imei":"863911223345",
	  		"msisdn":"13921320011",
	  		"gpslat":32.0001,
	  		"gpslng":120.001,
	  		"gpsalt":1.0,
	  		"speed":0.0
	  		}
	 */
	@ApiOperation(value = "坐标数据上报", notes = "GPS坐标上报")
	@RequestMapping(value={"/coordreport"})
	@ResponseBody
	public ApiResult<?> coordreport(HttpServletRequest request) throws Exception{		
		ApiResult<String> ret = new ApiResult<String>();
		String jsonStr = "";
		try{
			jsonStr = this.getJsonParam(request);
			logger.info("datareport json str: "+jsonStr);
			JSONObject json = new JSONObject(jsonStr);
			LocMessageBean locMsg = this.parseJson2LocMsg(json);
			if(locMsg != null){
				String pubMsg = JsonUtils.toJson(locMsg);
				this.parserContainer.getClaaLocGpsCoordPublisher().publishMessage(pubMsg);
				logger.info("[*^_^*]Publish mobiles loc gps coordinate data ok: " + pubMsg);
			} 				
			ret.setData("");
			ret.setResult(ApiResult.RET_SUCC);
			ret.setRemark("success");
		}catch(Exception e){
			logger.error("datareport process failed: "+jsonStr, e);
			ret.setResult(ApiResult.RET_FAIL);
			ret.setRemark(e.getMessage());
		}
		return ret;		
	}
	

	// 解析单个metedata对象. 
	private LocMessageBean parseJson2LocMsg(JSONObject json) throws Exception {
		HashMap<String, Object> data = new HashMap<String, Object>();
		Iterator<?> it = json.keys();
		// 遍历jsonObject数据，添加到Map对象
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			Object value = json.get(key);
			data.put(key, value);
		}
			
		LocMessageBean locMsg = new LocMessageBean();	
		String deveui = data.remove(IMEI_CODE).toString();
		if(deveui == null || deveui.length() == 0){
			return null;
		}
		DevBase devBase = cacheManager.getDev(deveui);
		if(devBase == null){
			return null;
		}
		try{
			locMsg.setDeveui(deveui);
			locMsg.setReport_time(data.remove(REPORT_TIME).toString());
			locMsg.setLon(NumUtils.parseDouble(data.remove(LNG).toString(),254));
			locMsg.setLat(NumUtils.parseDouble(data.remove(LAT).toString(),254));
			locMsg.setIn_out_flag(LocMessageBean.FLAG_OUT);
			locMsg.setZone_id(-1);
			locMsg.setProjectid(devBase.getProjectId());

			return locMsg;
		}catch (Exception e) {
			return null;
		}
	}
		
	
}
